#include "netradio.h"
#include "ui_netradio.h"

NetRadio::NetRadio(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NetRadio)
{
    ui->setupUi(this);


    pic = new QPixmap("icon/picture.png");
    *pic = pic->scaled(ui->Picture->size(), Qt::IgnoreAspectRatio);
    ui->Picture->setPixmap(*pic);
    ads = new QPixmap("icon/ads.png");
    *ads = ads->scaled(ui->label_ads->size(), Qt::IgnoreAspectRatio);
    ui->label_ads->setPixmap(*ads);

    ui->StarButton->setFlat(true);
    pixstart = new QPixmap("icon/start.png");
    pixstop = new QPixmap("icon/stop.png");
    ui->StarButton->setFixedSize(pixstart->width(), pixstart->height());
    ui->StarButton->setIcon(*pixstart);
    ui->StarButton->setIconSize(QSize(ui->StarButton->width(), ui->StarButton->height()));


    ui->PrevButton->setFlat(true);
    pixp = new QPixmap("icon/prev.png");
    //ui->PrevButton->setFixedSize(pixp->width(), pixp->height());
    *pixp = pixp->scaled(ui->PrevButton->size(), Qt::IgnoreAspectRatio);
    ui->PrevButton->setIcon(*pixp);
    ui->PrevButton->setIconSize(QSize(ui->PrevButton->width(), ui->PrevButton->height()));

    ui->NextButton->setFlat(true);
    pixn = new QPixmap("icon/next.png");
    //ui->NextButton->setFixedSize(pixn->width(), pixn->height());
    *pixn = pixn->scaled(ui->NextButton->size(), Qt::IgnoreAspectRatio);
    ui->NextButton->setIcon(*pixn);
    ui->NextButton->setIconSize(QSize(ui->NextButton->width(), ui->NextButton->height()));

    ui->ChList->setReadOnly(true);

    process = new QProcess();
    process->setProcessChannelMode(QProcess::MergedChannels);

    connect(ui->StarButton, SIGNAL(clicked()), this, SLOT(StartButton_clicked()));
    connect(ui->PrevButton, SIGNAL(clicked()), this, SLOT(PrevButton_clicked()));
    connect(ui->NextButton, SIGNAL(clicked()), this, SLOT(NextButton_clicked()));
    connect(process, SIGNAL(readyRead()), this, SLOT(back_message_slot()));

    curchn = 0;
    flag = 0;
    memset(input, 0, 5);
    sprintf(input, "%d", curchn);
    ui->Current->display(QString(input));

    time = new QTime();
    TimeString = new QString();
    timer = new QTimer();
    timer->setInterval(1000);
    timer->start(500);
    connect(timer,SIGNAL(timeout()),this, SLOT(updata_time()));
    ui->TimeLcd->setDigitCount(8);
}

NetRadio::~NetRadio()
{
    if (flag == 1)
    {
        flag = 0;
        curchn = 0;
        memset(input, 0, 5);
        sprintf(input, "%d\n", curchn);
        process->write(input);

        memset(input, 0, 5);
        sprintf(input, "%d", curchn);
        ui->Current->display(QString(input));

        ui->StarButton->setFixedSize(pixstart->width(), pixstart->height());
        ui->StarButton->setIcon(*pixstart);
        ui->StarButton->setIconSize(QSize(ui->StarButton->width(), ui->StarButton->height()));
    }
    delete ui;
}

void NetRadio::updata_time()
{
    *time = time->currentTime();
    TimeString->clear();
    TimeString->append(time->toString("hh:mm:ss"));
    ui->TimeLcd->display(*TimeString);
}

void NetRadio::StartButton_clicked()
{
    if (flag == 0) {
        process->start("./client");
        flag = 1;
        curchn = 1;
        memset(input, 0, 5);
        sprintf(input, "%d", curchn);
        ui->Current->display(QString(input));

        ui->StarButton->setFixedSize(pixstop->width(), pixstop->height());
        ui->StarButton->setIcon(*pixstop);
        ui->StarButton->setIconSize(QSize(ui->StarButton->width(), ui->StarButton->height()));
    } else if (flag == 1) {
        flag = 0;
        curchn = 0;
        memset(input, 0, 5);
        sprintf(input, "%d\n", curchn);
        process->write(input);

        memset(input, 0, 5);
        sprintf(input, "%d", curchn);
        ui->Current->display(QString(input));

        ui->StarButton->setFixedSize(pixstart->width(), pixstart->height());
        ui->StarButton->setIcon(*pixstart);
        ui->StarButton->setIconSize(QSize(ui->StarButton->width(), ui->StarButton->height()));
    }
}

void NetRadio::back_message_slot()
{
    printf("message\n");
    QString mes(process->readAll());
    ui->ChList->setText(mes);
}

void NetRadio::NextButton_clicked()
{
    if (flag ==1)
    {
        curchn += 1;
        if (curchn > 5)
            curchn = 1;
        memset(input, 0, 5);
        sprintf(input, "%d\n", curchn);
        process->write(input);

        memset(input, 0, 5);
        sprintf(input, "%d", curchn);
        ui->Current->display(QString(input));
    }
}

void NetRadio::PrevButton_clicked()
{
    if (flag ==1)
    {
        curchn -= 1;
        if (curchn < 1)
            curchn = 5;
        memset(input, 0, 5);
        sprintf(input, "%d\n", curchn);
        process->write(input);

        memset(input, 0, 5);
        sprintf(input, "%d", curchn);
        ui->Current->display(QString(input));
    }
}
